#!/usr/bin/env python

import os, sys
import matplotlib.pylab as plt
from astropy.table import Table
from argparse import ArgumentParser
import numpy as np

def plot_qe(qe_data_file, chip_sn=None):
    data = Table.read(qe_data_file, format='ipac')
    wave = np.array(data['wave'])
    qe   = np.array(data['qe'])
    qe_corr = np.array(data['qe_corr'])
    
    max_wave = np.max(wave)
    wwave = []
    qqe = []
    for i in range(len(wave)):
        wwave.append(wave[i])
        qqe.append(qe_corr[i])
        if wave[i] == max_wave:
            break

    lbl = 'UNKNOWN'
    if chip_sn is not None:
        lbl = chip_sn

    plt.figure(figsize=(8,6))
    plt.plot(wwave, qqe, 'b-o', label=lbl)
    plt.ylim(0,100)
    plt.grid()
    plt.xlabel('wavelength (nm)', fontsize=14)
    plt.ylabel('quantum efficiency (%)', fontsize=14)
    plt.yticks(np.arange(0,101,10))
    plt.legend(loc='best', fontsize=14)

    if chip_sn is not None:
        plt.savefig('{}.png'.format(chip_sn))
    
    plt.show()

if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('qe_data', type=str,
                        help='file name of qe data')
    parser.add_argument('--chip_sn', '-c', default=None,
                        help='serieal number of the CCD chip')
    args = parser.parse_args()
    plot_qe(qe_data_file=args.qe_data, chip_sn=args.chip_sn)


